﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Threading;


namespace SlabInspection
{
    public partial class mainForm : Form
    {
        public Point3D[] points = null;//点坐标的结构体数组
        public StreamReader myStream = null;
        private CSlab slab = null;
        Point[] PaintPoints = null;
        int baseX = 40;
        int baseY = 41+75;
        int radius = 10;
        int Rows;
        private Bitmap _unitmap;
        private bool _isAnalysised = false;

        public mainForm()
        {
            InitializeComponent();
            _initParameter();
            //this.Size = Screen.PrimaryScreen.Bounds.Size;
            this.WindowState = FormWindowState.Normal;
            //this.MinimizeBox = false;
            //tabControl1.Location.X = (this.Size.Width - tabControl1.Size.Width) / 2;
            //pictureBox_Plane.Left = (tabPage_Plane.ClientSize.Width - pictureBox_Plane.Width) / 2;

        }

        //读取配置文件的参数
        private void _initParameter()
        {
            XmlDocument xd = new XmlDocument();
            xd.Load("Parameter.xml");
            textBox_SavePath.Text = xd.GetElementsByTagName("SavePath")[0].InnerText.ToString();
            numericUpDown_Restrict.Value = Convert.ToDecimal(xd.GetElementsByTagName("Restrict")[0].InnerText);
            textBox_Company.Text = xd.GetElementsByTagName("Company")[0].InnerText;
            textBox_Surveyor.Text = xd.GetElementsByTagName("Surveyor")[0].InnerText;
        }

        //导入数据按钮
        private void button_ImportData_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = @"d:\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|csv files (*.csv)|*.csv|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.FileName = "*.*";

            tabControl1.SelectTab("tabPage_Data");
            dataGridView1.Rows.Clear();
            dataGridView1.ReadOnly = true;
            String fn;
            //导入csv格式的数据文件。格式：点名，X,Y,Z。一个文件就是一块板的数据，文件名就是板编号。
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog1.OpenFile() != null)
                {
                    fn = openFileDialog1.FileName;
                    fn = fn.Substring(fn.LastIndexOf('\\') + 1);
                    fn = fn.Remove(fn.LastIndexOf('.'));
                    textBox_SlabIndex.Text = fn;
                    StreamReader sr = File.OpenText(openFileDialog1.FileName);
                    String sLine;
                    int ii = 0;

                    //求出有多少行数据，即多少个点，并把数据绑定到DataGridView中。
                    while ((sLine = sr.ReadLine()) != null)
                    {
                        string[] s = sLine.Split(',');
                        dataGridView1.Rows.Add(s);
                        ii++;
                    }
                    sr.Close();
                }
                button_analysis.Visible = true;
                Button_Save.Visible = false;
            }
            else
            {
                button_analysis.Visible = false;
                Button_Save.Visible = false;
            }
            this.toolStripStatusLabel1.Text = "导入数据成功！";
        }

        //手工录入数据
        private void button_Input_Click(object sender, EventArgs e)
        {
            tabControl1.SelectTab("tabPage_Data");
            //dataGridView1.Rows.Clear();
            dataGridView1.ReadOnly = false;
            button_analysis.Visible = true;
            Button_Save.Visible = false;
        }

        //分析按钮
        private void button_Analysis_Click(object sender, EventArgs e)
        {
            _Analysis_status();
        }

        private bool _Analysis()
        {
            //检查数据：点数应该是24,32,40
            if (!((dataGridView1.RowCount - 7) == 24 || (dataGridView1.RowCount - 7) == 32 || (dataGridView1.RowCount - 7) == 40))
            {
                //MessageBox.Show("板的规格应该是24、32、40个孔");
                return false;
            }
            Rows = (dataGridView1.RowCount - 6) / 4;
            switch(dataGridView1.RowCount - 7)
            {
                case 24: _unitmap = Properties.Resources.unit6; break;
                case 32: _unitmap = Properties.Resources.unit8; break;
                case 40: _unitmap = Properties.Resources.unit10; break;
                default: { MessageBox.Show("没有对应规格的板位图"); return false; }
            }
            //Rows = (dataGridView1.RowCount - 1) / 4;
            //_unitmap = Properties.Resources.unit8;
            points = new Point3D[dataGridView1.RowCount - 1];
            //确定数组长度，主要是方便后面的程序处理。StreamWriter 不能重新定位到文件头
            //todo:
            for (int i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                points[i].x = Convert.ToDouble(dataGridView1.Rows[i].Cells[1].Value) * 1000;
                points[i].y = Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value) * 1000;
                points[i].z = Convert.ToDouble(dataGridView1.Rows[i].Cells[3].Value) * 1000;
            }

            slab = new CSlab(points);
            slab.Analysis();

            richTextBox_datafile.Text += "实测点原始\n";
            richTextBox_datafile.Text += Utility.FormatPointsToString(points);
            richTextBox_datafile.Text += "\n\n实测点slab\n";
            richTextBox_datafile.Text += Utility.FormatPointsToString(slab.points);
            richTextBox_datafile.Text += "\n\n投影点";
            richTextBox_datafile.Text += Utility.FormatPointsToString(slab.footPoints);
            richTextBox_datafile.Text += "\n\n分了行的投影点";
            richTextBox_datafile.Text += Utility.FormatPointsToString(slab.footLinePoints);
            richTextBox_datafile.Text += "\n\n面偏移量";
            richTextBox_datafile.Text += Utility.FormatPointsToString(slab.PlaneOffset);
            richTextBox_datafile.Text += "\n\n直线偏移量";
            richTextBox_datafile.Text += Utility.FormatPointsToString(slab.footLineOffsets);
            richTextBox_datafile.Text += "\n\n标架点";
            richTextBox_datafile.Text += Utility.FormatPointsToString(slab.ShelfPoints);
            richTextBox_datafile.Text += "\n\n最佳中线两点";
            richTextBox_datafile.Text += Utility.FormatPointsToString(slab._bestMiddleTwoPoint);
            richTextBox_datafile.Text += "\n\n对称偏移量 948 + 566 = 1514";
            richTextBox_datafile.Text += Utility.FormatPointsToString(slab.footSymmeOffsets);
            richTextBox_datafile.Text += "\n\n标架中间两点连线，与计算出的最佳中线的夹角";
            double angle = Geometry.GetCrossAngle(slab.footShelfPoints[4], slab.footShelfPoints[5], slab._bestMiddleTwoPoint[0], slab._bestMiddleTwoPoint[1]) * (180 / Math.PI) * 3600;
            richTextBox_datafile.Text += angle.ToString()+"\"";
            richTextBox_datafile.Text += "\n\n标架中间两点到理论中线的距离\n";
            double distance = Geometry.Pt2LineByTrigonometric(slab._bestMiddleTwoPoint[0],slab._bestMiddleTwoPoint[1],slab.ShelfPoints[4]);
            richTextBox_datafile.Text += distance.ToString() + "mm  ";
            distance = Geometry.Pt2LineByTrigonometric(slab._bestMiddleTwoPoint[0], slab._bestMiddleTwoPoint[1], slab.ShelfPoints[5]);
            richTextBox_datafile.Text += distance.ToString() + "mm  ";

            richTextBox_datafile.Text += "\n\n标架1、2号投影点连线的中点和5号标架投影的偏移\n" + slab.ShelfOffsets[0].ToString();
            richTextBox_datafile.Text += "\n\n标架3、4号投影点连线的中点和5号标架投影的偏移\n" + slab.ShelfOffsets[1].ToString();


            //取得小数点后位数
            for (int i = 0; i < slab.PlaneOffset.Length; i++)
            {
                slab.PlaneOffset[i] = Math.Round(slab.PlaneOffset[i],2);
            }
            for (int i = 0; i < slab.footLineOffsets.GetLength(0); i++)
            {
                for (int j = 0; j < slab.footLineOffsets.GetLength(1); j++)
                {
                    slab.footLineOffsets[i,j] = Math.Round(slab.footLineOffsets[i,j], 2);
                }
            }
            for (int i = 0; i < slab.footSymmeOffsets.GetLength(0); i++)
            {
                for (int j = 0; j < slab.footSymmeOffsets.GetLength(1); j++)
                {
                    slab.footSymmeOffsets[i, j] = Math.Round(slab.footSymmeOffsets[i, j], 2);
                }
            }
            for (int i = 0; i < slab._ShelfAngle.Length; i++)
            {
                slab._ShelfAngle[i] = Math.Round(slab._ShelfAngle[i], 2);
            }



            PaintPoints = new Point[points.Length - 6];
            for (int i = 0; i < PaintPoints.Length; i++)
            {
                PaintPoints[i].X = i / 4 * 95 + baseX;
                if (i % 4 == 1)
                    PaintPoints[i].Y = i % 4 + 57 + baseY;
                if (i % 4 == 2)
                    PaintPoints[i].Y = i % 4 + 226 + baseY;
                if (i % 4 == 3)
                    PaintPoints[i].Y = i % 4 + 283 + baseY;
                if (i % 4 == 0)
                    PaintPoints[i].Y = baseY ;
            }
            Button_Save.Visible = true;
            _isAnalysised = true;
            return true;
            
        }
        
        //保存按钮
        private void button_Save_Click(object sender, EventArgs e)
        {
            Bitmap bitmap = new Bitmap(_unitmap);
            bitmap = _drawing_plane(bitmap);
            bitmap.Save(textBox_SavePath.Text.ToString() + "\\" + textBox_SlabIndex.Text.ToString() + "_plane.jpg");

            Bitmap bitmap2 = new Bitmap(_unitmap);
            bitmap2 = _drawing_line(bitmap2);
            bitmap2.Save(textBox_SavePath.Text.ToString() + "\\" + textBox_SlabIndex.Text.ToString() + "_line.jpg");

            Bitmap bitmap3 = new Bitmap(_unitmap);
            bitmap3 = _drawing_symme(bitmap3);
            bitmap3.Save(textBox_SavePath.Text.ToString() + "\\" + textBox_SlabIndex.Text.ToString() + "_symme.jpg");

            Bitmap bitmap4 = new Bitmap(_unitmap);
            bitmap4 = _drawing_shelf(bitmap4);
            bitmap4.Save(textBox_SavePath.Text.ToString() + "\\" + textBox_SlabIndex.Text.ToString() + "_shelf.jpg");
        }

        //作图_共面性
        private Bitmap _drawing_plane(Bitmap bitmap)
        {
            pictureBox_Plane.Image = bitmap;
            //pictureBox_Plane.SizeMode = PictureBoxSizeMode.StretchImage;
            Graphics g = Graphics.FromImage(bitmap);
            Pen p = new Pen(Color.Black);
            SolidBrush sBrush = new SolidBrush(Color.Blue);
            SolidBrush headBrush = new SolidBrush(Color.Black);
            Font drawFont = new Font("Arial", 12);

            g.DrawString("检测类型：共面性", drawFont, headBrush, baseX, baseY - 85);
            g.DrawString("板 编 号：" + textBox_SlabIndex.Text, drawFont, headBrush, baseX, baseY - 65);
            g.DrawString("施工单位：" + textBox_Company.Text, drawFont, headBrush, baseX + 220, baseY - 65);
            g.DrawString("测量人员：" + textBox_Surveyor.Text, drawFont, headBrush, baseX + 400, baseY - 65);
            g.DrawString("制表时间：" + DateTime.Now.ToShortDateString(), drawFont, headBrush, baseX + 550, baseY - 65);

            for (int i = 0; i < PaintPoints.Length; i++)
            {
                g.DrawEllipse(p, PaintPoints[i].X, PaintPoints[i].Y, radius, radius);
                if (Math.Abs(slab.PlaneOffset[i]) > Convert.ToDouble(numericUpDown_Restrict.Value))
                {
                    if (slab.PlaneOffset[i] > 0.0)
                    {
                        g.DrawString("+" + slab.PlaneOffset[i].ToString(), drawFont, new SolidBrush(Color.Red), PaintPoints[i].X + 26, PaintPoints[i].Y + 4);
                    }
                    else
                    {
                        g.DrawString(slab.PlaneOffset[i].ToString(), drawFont, new SolidBrush(Color.Red), PaintPoints[i].X + 26, PaintPoints[i].Y + 4);
                    }
                    continue;
                }
                else
                {
                    if (slab.PlaneOffset[i] > 0.0)
                    {
                        g.DrawString("+" + slab.PlaneOffset[i].ToString(), drawFont, new SolidBrush(Color.Green), PaintPoints[i].X + 26, PaintPoints[i].Y + 4);
                    }
                    else
                    {
                        g.DrawString(slab.PlaneOffset[i].ToString(), drawFont, new SolidBrush(Color.Green), PaintPoints[i].X + 26, PaintPoints[i].Y + 4);
                    }
                }
                
            }

            //基准平面的四个作图点
            Point[] ps = new Point[4];
            ps[0] = new Point(PaintPoints[slab.p1].X, PaintPoints[slab.p1].Y);
            ps[1] = new Point(PaintPoints[slab.p2].X, PaintPoints[slab.p2].Y);
            ps[2] = new Point(PaintPoints[slab.p3].X, PaintPoints[slab.p3].Y);
            ps[3] = new Point(PaintPoints[slab.p1].X, PaintPoints[slab.p1].Y);

            {
                g.FillEllipse(new SolidBrush(Color.Blue), PaintPoints[slab.p1].X, PaintPoints[slab.p1].Y, radius, radius);
                g.FillEllipse(new SolidBrush(Color.Blue), PaintPoints[slab.p2].X, PaintPoints[slab.p2].Y, radius, radius);
                g.FillEllipse(new SolidBrush(Color.Blue), PaintPoints[slab.p3].X, PaintPoints[slab.p3].Y, radius, radius);

                g.FillPolygon(new SolidBrush(Color.FromArgb(60,Color.Black)), ps);
            }
            return bitmap;
        }
        //作图_直线性
        private Bitmap _drawing_line(Bitmap bitmap)
        {
            pictureBox_Line.Image = bitmap;
            //pictureBox_Line.SizeMode = PictureBoxSizeMode.StretchImage;
            Graphics g = Graphics.FromImage(bitmap);
            Pen p = new Pen(Color.Black);
            SolidBrush sBrush = new SolidBrush(Color.Blue);
            SolidBrush headBrush = new SolidBrush(Color.Black);
            Font drawFont = new Font("Arial", 12);

            g.DrawString("检测类型：直线性", drawFont, headBrush, baseX, baseY - 85);
            g.DrawString("板 编 号：" + textBox_SlabIndex.Text, drawFont, headBrush, baseX, baseY - 65);
            g.DrawString("施工单位：" + textBox_Company.Text, drawFont, headBrush, baseX + 220, baseY - 65);
            g.DrawString("测量人员：" + textBox_Surveyor.Text, drawFont, headBrush, baseX + 400, baseY - 65);
            g.DrawString("制表时间：" + DateTime.Now.ToShortDateString(), drawFont, headBrush, baseX + 550, baseY - 65);

            string offset;
            Point[] line2p = new Point [2];
            for (int i = 0; i < slab.footLineOffsets.GetLength(0); i++)
            {

                for (int j = 0; j < slab.footLineOffsets.GetLength(1); j++)
                {
                    if (slab.footLineOffsets[i, j] > 0.0)
                    {
                        offset = "+" + slab.footLineOffsets[i, j].ToString();
                    }
                    else
                    {
                        offset = slab.footLineOffsets[i, j].ToString();
                    }
                    if (offset == "0")
                    {
                        g.FillEllipse(sBrush, PaintPoints[j * 4 + i].X, PaintPoints[j * 4 + i].Y, radius, radius);
                    }
                    if (Math.Abs(slab.footLineOffsets[i, j]) > Convert.ToDouble(numericUpDown_Restrict.Value))
                    {
                        g.DrawString(offset, drawFont, new SolidBrush(Color.Red), PaintPoints[j * 4 + i].X + 26, PaintPoints[j * 4 + i].Y + 6);
                        continue;
                    }
                    g.DrawString(offset, drawFont, new SolidBrush(Color.Green), PaintPoints[j * 4 + i].X + 26, PaintPoints[j * 4 + i].Y + 6);
                }
            }
            ////划线
            //for (int i = 0; i < slab.p2ps.GetLength(0); i++)
            //{
            //    for (int j = 0; j < slab.p2ps.GetLength(1); j++)
            //    {
            //        g.DrawLine(new Pen(Color.Red), PaintPoints[slab.p2ps[i, 0] + i * 4].X, PaintPoints[slab.p2ps[i, 0] + i * 4].Y,PaintPoints[slab.p2ps[i, 1] + i * 4].X, PaintPoints[slab.p2ps[i, 1] + i * 4].Y);
            //    }
            //}
            g.DrawLine(new Pen(Color.Red, 2), 0, 120, bitmap.Size.Width, 120);
            g.DrawLine(new Pen(Color.Red, 2), 0, 180, bitmap.Size.Width, 180);
            g.DrawLine(new Pen(Color.Red, 2), 0, 350, bitmap.Size.Width, 350);
            g.DrawLine(new Pen(Color.Red, 2), 0, 407, bitmap.Size.Width, 407);

            return bitmap;
        }

        //对称性作图
        private Bitmap _drawing_symme(Bitmap bitmap)
        {
            pictureBox_Symme.Image = bitmap;
            //pictureBox_Symme.SizeMode = PictureBoxSizeMode.StretchImage;
            Graphics g = Graphics.FromImage(bitmap);
            Pen p = new Pen(Color.Black);
            SolidBrush sBrush = new SolidBrush(Color.Blue);
            SolidBrush headBrush = new SolidBrush(Color.Black);
            SolidBrush angleBrush = new SolidBrush(Color.Red);
            Font drawFont = new Font("Arial", 12);
            Font drawFont2 = new Font("Arial", 10);
            SolidBrush headBrush2 = new SolidBrush(Color.Red);

            g.DrawString("检测类型：对称性（平行性）", drawFont, headBrush, baseX, baseY - 85);
            g.DrawString("板 编 号：" + textBox_SlabIndex.Text, drawFont, headBrush, baseX, baseY - 65);
            g.DrawString("施工单位：" + textBox_Company.Text, drawFont, headBrush, baseX + 220, baseY - 65);
            g.DrawString("测量人员：" + textBox_Surveyor.Text, drawFont, headBrush, baseX + 400, baseY - 65);
            g.DrawString("制表时间：" + DateTime.Now.ToShortDateString(), drawFont, headBrush, baseX + 550, baseY - 65);

            g.DrawLine(new Pen(Color.Red, 2), 0, 120, bitmap.Size.Width, 120);
            g.DrawString("直线一", drawFont2, angleBrush, 70, 100);
            g.DrawLine(new Pen(Color.Red, 2), 0, 180, bitmap.Size.Width, 180);
            g.DrawString("直线二", drawFont2, angleBrush, 70, 160);
            g.DrawLine(new Pen(Color.Red, 2), 0, 264, bitmap.Size.Width, 264);
            g.DrawString("中线", drawFont2, angleBrush, 55, 242);
            g.DrawLine(new Pen(Color.Red, 2), 0, 350, bitmap.Size.Width, 350);
            g.DrawString("直线三", drawFont2, angleBrush, 70, 330);
            g.DrawLine(new Pen(Color.Red, 2), 0, 407, bitmap.Size.Width, 407);
            g.DrawString("直线四", drawFont2, angleBrush, 70, 390);

            g.DrawString("直线一与中线的夹角：" + slab.ange[0].ToString() + "\"", drawFont, angleBrush, baseX + 170, baseY - 85 + 190);
            g.DrawString("直线二与中线的夹角：" + slab.ange[1].ToString() + "\"", drawFont, angleBrush, baseX + 170, baseY - 65 + 190);
            g.DrawString("直线三与中线的夹角：" + slab.ange[2].ToString() + "\"", drawFont, angleBrush, baseX + 170, baseY - 45 + 198);
            g.DrawString("直线四与中线的夹角：" + slab.ange[3].ToString() + "\"", drawFont, angleBrush, baseX + 170, baseY - 25 + 198);

            //标准948 + 566 = 1514
            for (int i = 0; i < slab.footSymmeOffsets.GetLength(0); i++)
            {
                for (int j = 0; j < slab.footSymmeOffsets.GetLength(1); j++)
                {
                    if (i % 4 == 0)
                    {
                        g.DrawString(Math.Round((slab.footSymmeOffsets[i, j] - 948),2).ToString() , drawFont, new SolidBrush(Color.Green), PaintPoints[j * 4 + i].X + 26, PaintPoints[j * 4 + i].Y + 6);
                    }
                    if (i % 4 == 1)
                    {
                        g.DrawString(Math.Round((slab.footSymmeOffsets[i, j] - 566), 2).ToString(), drawFont, new SolidBrush(Color.Green), PaintPoints[j * 4 + i].X + 26, PaintPoints[j * 4 + i].Y + 6);
                    }
                    if (i % 4 == 2)
                    {
                        g.DrawString(Math.Round((slab.footSymmeOffsets[i, j] - 566), 2).ToString(), drawFont, new SolidBrush(Color.Green), PaintPoints[j * 4 + i].X + 26, PaintPoints[j * 4 + i].Y + 6);
                    }
                    if (i % 4 == 3)
                    {
                        g.DrawString(Math.Round((slab.footSymmeOffsets[i, j] - 948), 2).ToString(), drawFont, new SolidBrush(Color.Green), PaintPoints[j * 4 + i].X + 26, PaintPoints[j * 4 + i].Y + 6);
                    }
                }
            }
            return bitmap;
        }

        //作图_标架分析
        private Bitmap _drawing_shelf(Bitmap bitmap)
        {
            pictureBox_Shelf.Image = bitmap;
           // pictureBox_Shelf.SizeMode = PictureBoxSizeMode.StretchImage;
            Graphics g = Graphics.FromImage(bitmap);
            Pen p = new Pen(Color.Red,2);
            SolidBrush headBrush = new SolidBrush(Color.Black);
            SolidBrush headBrush2 = new SolidBrush(Color.Red);
            Font drawFont = new Font("Arial", 12);

            g.DrawString("检测类型：标架分析", drawFont, headBrush, baseX, baseY - 85);
            g.DrawString("板 编 号：" + textBox_SlabIndex.Text, drawFont, headBrush, baseX, baseY - 65);
            g.DrawString("施工单位：" + textBox_Company.Text, drawFont, headBrush, baseX + 220, baseY - 65);
            g.DrawString("测量人员：" + textBox_Surveyor.Text, drawFont, headBrush, baseX + 400, baseY - 65);
            g.DrawString("制表时间：" + DateTime.Now.ToShortDateString(), drawFont, headBrush, baseX + 550, baseY - 65);

            g.DrawLine(p, 0, 265, bitmap.Size.Width, 265);
            g.DrawLine(p, new Point(142, baseY - 20), new Point(142, bitmap.Size.Height - 40));
            g.DrawLine(p, new Point(615, baseY - 20), new Point(615, bitmap.Size.Height - 40));
            g.DrawString(slab._ShelfAngle[1].ToString() + "°", drawFont, headBrush2, new Point(150, bitmap.Size.Height / 2));
            g.DrawArc(p, 122, 245, 40, 40, 270, 90);
            g.DrawString("1、2号标架的连线中点与5号标架偏移:" + slab.ShelfOffsets[0].ToString() + "mm", drawFont, headBrush2, new Point(210, 240));
            g.DrawString(slab._ShelfAngle[0].ToString() + "°", drawFont, headBrush2, new Point(620, bitmap.Size.Height / 2));
            g.DrawArc(p, 595, 245, 40, 40, 270, 90);
            g.DrawString("3、4号标架的连线中点与6号标架偏移:" + slab.ShelfOffsets[1].ToString() + "mm", drawFont, headBrush2, new Point(210, 280));
            return bitmap;
        }


        

        private void tabPage_Plane_Paint(object sender, PaintEventArgs e)
        {
            Bitmap bitmap = new Bitmap(_unitmap);
            _drawing_plane(bitmap);

        }

        private void tabPage_Line_Paint(object sender, PaintEventArgs e)
        {
            Bitmap bitmap = new Bitmap(_unitmap);
            _drawing_line(bitmap);
        }

        private void pictureBox_Symme_Paint(object sender, PaintEventArgs e)
        {
            Bitmap bitmap = new Bitmap(_unitmap);
            _drawing_symme(bitmap);
        }

        private void tabPage_Shelf_Paint(object sender, PaintEventArgs e)
        {
            Bitmap bitmap = new Bitmap(_unitmap);
            _drawing_shelf(bitmap);
        }

        //退出
        private void button_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void tabPage_Plane_Enter(object sender, EventArgs e)
        {
            _Analysis_status();
        }

        private void tabPage_Line_Enter(object sender, EventArgs e)
        {
            _Analysis_status();
        }

        private void tabPage_Symmetry_Enter(object sender, EventArgs e)
        {
            _Analysis_status();
        }

        private void tabPage_Shelf_Enter(object sender, EventArgs e)
        {
            _Analysis_status();
        }

        private void _Analysis_status()
        {
            Button_Save.Visible = true;
            if(_isAnalysised)
            {
                return;
            }
            if (_Analysis() == false)
            {
                MessageBox.Show("分析失败，请正确导入数据！");
                tabControl1.SelectTab("tabPage_Data");
                dataGridView1.Rows.Clear();
                button_analysis.Visible = false;
                Button_Save.Visible = false;
                textBox_SlabIndex.Text = "";
                return;
            }
        }

        private void button_SavePath_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_SavePath.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        //保存修改后的参数
        private void button_SaveParameter_Click(object sender, EventArgs e)
        {
            XmlDocument xd = new XmlDocument();
            xd.Load("Parameter.xml");
            XmlNode xn = xd.DocumentElement;
            xn.SelectSingleNode("SavePath").InnerText = textBox_SavePath.Text;
            xd.GetElementsByTagName("Restrict")[0].InnerText = numericUpDown_Restrict.Value.ToString();
            xd.SelectSingleNode("Company").InnerText = textBox_Company.Text;
            xd.SelectSingleNode("Surveyor").InnerText = textBox_Surveyor.Text;
            xd.Save("Parameter.xml");
        }
    }
}